package me.zk.practice1.hw;

import java.util.*;

/**
 * @author zhangkai
 * @date 2023/8/8 11:22
 */
public class HWBJ34 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        int[][] num = new int[n][2];
        Map<Integer,String> map = new HashMap<>();

        for (int i = 0; i<n;i++){
            int sum = 0;
            int index = -1;
            String str = scanner.nextLine();
            map.put(i+1,str);
            for (int j = 0; j < str.length(); j++) {
                if (str.charAt(j) == 'M') {
                    sum += Integer.parseInt(str.substring(index+1,j));
                    index = j;
                } else if (str.charAt(j) == 'G') {
                    sum+= Integer.parseInt(str.substring(index+1,j))*1024;
                    index = j;
                }else if (str.charAt(j) == 'T') {
                    sum+= Integer.parseInt(str.substring(index+1,j))*1024*1024;
                    index = j;
                }

            }
            num[i][0] = i+1;
            num[i][1] = sum;
        }
        Arrays.sort(num, (o1, o2) -> o1[1]==o2[1]?o1[0]-o2[0]:o1[1]-o2[1]);

        for (int i = 0; i < n; i++) {
            System.out.println(map.get(num[i][0]));
        }
    }
}
